﻿// Copyright (C) 2009 - 2013 EvTech Project
//
// This program is free software: you can redistribute it and/or modify
// it under the +terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using System.Diagnostics;

namespace Piirto
{
    public partial class DrawForm : Form
    {
        private PiirtoMainForm piirtoMainForm;
        private const int IMAGE_SIZE = 800;
        private enum Tool { FREEHAND, LINE, CIRCLE, RECTANGLE, FILL, ERASER, STAMPER, MOVE, RESIZE, ROTATE, TEXT};
        private Tool selectedTool;
        private List<DrawTool> drawHistory;
        private DrawTool tmpDraw;
        private Bitmap originalImage;
        private Bitmap currentImage;
        private SelectImageForm selectImageDlg;
        private SelectImageForm selectStampDlg;
        private int penWidth, minPenWidth, maxPenWidth;
        private Cursor actionCursor;
        

        public DrawForm()
        {
            InitializeComponent();
        }

        public DrawForm(PiirtoMainForm piirtoMainForm)
        {
            InitializeComponent();
            this.piirtoMainForm = piirtoMainForm;
            colorControl1.ColorSelected += colorControl_Click;
            this.currentColorImage.BackColor = this.colorControl1.getSelectedColor();
            selectTool(Tool.FREEHAND);
            drawHistory = new List<DrawTool>();
            tmpDraw = null;
            this.selectImageDlg = new SelectImageForm("./images");
            this.selectImageDlg.addImageFolder("./saved");
            this.selectStampDlg = new SelectImageForm("./stamps");
            originalImage = new Bitmap(IMAGE_SIZE, IMAGE_SIZE * paintImage.Height / paintImage.Width);
            Graphics g = Graphics.FromImage(originalImage);
            g.Clear(Color.White);
            g.Dispose();
            paintImage.Image = new Bitmap(originalImage);
            currentImage = new Bitmap(originalImage);
            penWidth = 5;
            minPenWidth = 1;
            maxPenWidth = Math.Max(originalImage.Width, originalImage.Height) / 20;
        }

        private void DrawForm_Load(object sender, EventArgs e)
        {
            updatePenWidthBox();
        }

        private void DrawForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            this.piirtoMainForm.Show();
        }

        private void selectTool(Tool tool)
        {
            if (tmpDraw != null)
            {
                if (tmpDraw is TextTool && ((TextTool)tmpDraw).setReady())
                {
                    drawHistory.Add(tmpDraw);
                    drawNew(tmpDraw);
                }
                tmpDraw = null;
                drawNew(tmpDraw);
            }
            this.btnFreeHand.FlatStyle = FlatStyle.Standard;
            this.btnLine.FlatStyle = FlatStyle.Standard;
            this.btnCircle.FlatStyle = FlatStyle.Standard;
            this.btnRectangle.FlatStyle = FlatStyle.Standard;
            this.btnFill.FlatStyle = FlatStyle.Standard;
            this.btnEraser.FlatStyle = FlatStyle.Standard;
            this.btnStamper.FlatStyle = FlatStyle.Standard;
            this.btnMove.FlatStyle = FlatStyle.Standard;
            this.btnResize.FlatStyle = FlatStyle.Standard;
            this.btnRotate.FlatStyle = FlatStyle.Standard;
            this.btnText.FlatStyle = FlatStyle.Standard;
            selectedTool = tool;

            switch (selectedTool)
            {
                case Tool.FREEHAND:
                    this.btnFreeHand.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnFreeHand.BackgroundImage;
                    break;
                case Tool.LINE:
                    this.btnLine.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnLine.BackgroundImage;
                    break;
                case Tool.CIRCLE:
                    this.btnCircle.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnCircle.BackgroundImage;
                    break;
                case Tool.RECTANGLE:
                    this.btnRectangle.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnRectangle.BackgroundImage;
                    break;
                case Tool.FILL:
                    this.btnFill.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnFill.BackgroundImage;
                    break;
                case Tool.ERASER:
                    this.btnEraser.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnEraser.BackgroundImage;
                    break;
                case Tool.STAMPER:
                    this.btnStamper.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    if (selectStampDlg.getSelectedImage() != null)
                    {
                        this.currentColorImage.Image = selectStampDlg.getSelectedImage();
                    }
                    else
                    {
                        this.currentColorImage.Image = this.btnStamper.BackgroundImage;
                    }
                    break;
                case Tool.MOVE:
                    this.btnMove.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnMove.BackgroundImage;
                    break;
                case Tool.RESIZE:
                    this.btnResize.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnResize.BackgroundImage;
                    break;
                case Tool.ROTATE:
                    this.btnRotate.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnRotate.BackgroundImage;
                    break;
                case Tool.TEXT:
                    this.btnText.FlatStyle = FlatStyle.Flat;
                    this.actionCursor = DefaultCursor;
                    this.currentColorImage.Image = this.btnText.BackgroundImage;
                    break;
            }
        }

        private void btnFreeHand_Click(object sender, EventArgs e)
        {
            selectTool(Tool.FREEHAND);
        }

        private void btnLine_Click(object sender, EventArgs e)
        {
            selectTool(Tool.LINE);
        }

        private void btnCircle_Click(object sender, EventArgs e)
        {
            selectTool(Tool.CIRCLE);
        }

        private void btnRectangle_Click(object sender, EventArgs e)
        {
            selectTool(Tool.RECTANGLE);
        }

        private void btnFill_Click(object sender, EventArgs e)
        {
            selectTool(Tool.FILL);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (tmpDraw != null)
            {
                if (tmpDraw is TextTool && ((TextTool)tmpDraw).setReady())
                {
                    drawHistory.Add(tmpDraw);
                    drawNew(tmpDraw);
                }
                tmpDraw = null;
            }
            this.Cursor = Cursors.WaitCursor;
            ImagePrintDocument doc = new ImagePrintDocument(paintImage.Image);

            try
            {
                doc.Print();
                this.Activate();
                this.Cursor = Cursors.Default;
                InformationDialog.Show("Tulostetaan");
            }
            catch (Exception)
            {
                this.Cursor = Cursors.Default;
                InformationDialog.Show("Tulostus ei onnistu");
            }
        }


        private void colorControl_Click(object sender, EventArgs e)
        {
            this.currentColorImage.BackColor = this.colorControl1.getSelectedColor();
            if (tmpDraw != null)
            {
                tmpDraw.setColor(this.colorControl1.getSelectedColor());
                drawTmp();
            }
            updatePenWidthBox();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tmpDraw != null)
            {
                if (tmpDraw is TextTool && ((TextTool)tmpDraw).setReady())
                {
                    drawHistory.Add(tmpDraw);
                    drawNew(tmpDraw);
                }
                tmpDraw = null;
            }
            TextInputDialog saveDialog = new TextInputDialog("Tallennetaan kuva", "Kirjoita nimesi", "Tallenna", "Peru");
            bool isSaved = false;
            while (!isSaved)
            {
                try
                {
                    string filename = "./saved/";
                    if (!Directory.Exists(filename))
                    {
                        Directory.CreateDirectory(filename);
                    }
                    saveDialog.ShowDialog();
                    if (saveDialog.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                    {
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    filename += saveDialog.WrittenText;
                    filename += DateTime.Now.ToString(" yyyy_MM_dd_HH_mm_ss") + ".bmp";
                    paintImage.Image.Save(filename);
                    isSaved = true;
                }
                catch (Exception ex)
                {
                    Console.Out.WriteLine(ex.StackTrace);
                }
            } 
            this.selectImageDlg.refreshImages();
            InformationDialog.Show("Kuva on tallennettu");
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (tmpDraw != null)
            {
                if (tmpDraw is TextTool && ((TextTool)tmpDraw).setReady())
                {
                    drawHistory.Add(tmpDraw);
                    drawNew(tmpDraw);
                }
                tmpDraw = null;
            }
            this.Hide();
            this.piirtoMainForm.Show();
        }

        private void redrawHistory()
        {
            this.currentImage = (Bitmap)originalImage.Clone();
            Graphics g = Graphics.FromImage(this.currentImage);
            foreach (DrawTool oldTool in drawHistory)
            {
                if (oldTool is FillTool)
                {
                    ((FillTool)oldTool).reFill(this.currentImage);
                }
                oldTool.Paint(g, 0, 0, 1, 0);
            }
            g.Dispose();
            this.paintImage.Image = (Bitmap)currentImage.Clone();
        }

        private void drawTmp()
        {
            this.paintImage.Image = (Bitmap)currentImage.Clone();
            if (tmpDraw == null)
            {
                return;
            }
            Graphics g = Graphics.FromImage(this.paintImage.Image);
            if (tmpDraw != null)
            {
                tmpDraw.Paint(g, 0, 0, 1, 0);
            }
            g.Dispose();
            this.paintImage.Refresh();
        }

        private void drawNew(DrawTool dTool)
        {
            Graphics g = Graphics.FromImage(this.currentImage);
            if (dTool != null)
            {
                dTool.Paint(g, 0, 0, 1, 0);
            }
            g.Dispose();
            this.paintImage.Image = (Bitmap)currentImage.Clone();
        }

        private void paintImage_MouseDown(object sender, MouseEventArgs e)
        {
            paintImage.Focus();
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            if (tmpDraw != null)
            {
                if (tmpDraw is TextTool && ((TextTool)tmpDraw).setReady())
                {
                    drawHistory.Add(tmpDraw);
                    drawNew(tmpDraw);
                }
                tmpDraw = null;
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                int x, y;

                if (paintImage.Image.Width / paintImage.Image.Height > paintImage.Width / paintImage.Height)
                {
                    x = e.X * paintImage.Image.Width / paintImage.Width;
                    y = (e.Y - (paintImage.Height - paintImage.Image.Height * paintImage.Width /
                        paintImage.Image.Width) / 2) * paintImage.Image.Width / paintImage.Width;
                }
                else
                {
                    x = (e.X - (paintImage.Width - paintImage.Image.Width * paintImage.Height /
                        paintImage.Image.Height) / 2) * paintImage.Image.Height / paintImage.Height;
                    y = e.Y * paintImage.Image.Height / paintImage.Height;
                }
                switch (selectedTool)
                {
                    case Tool.FREEHAND:
                        tmpDraw = new FreehandTool(this.colorControl1.getSelectedColor(), penWidth);
                        tmpDraw.newPoint(x, y);
                        break;
                    case Tool.LINE:
                        tmpDraw = new LineTool(this.colorControl1.getSelectedColor(), penWidth);
                        tmpDraw.newPoint(x, y);
                        break;
                    case Tool.CIRCLE:
                        tmpDraw = new CircleTool(this.colorControl1.getSelectedColor(), penWidth);
                        tmpDraw.newPoint(x, y);
                        break;
                    case Tool.RECTANGLE:
                        tmpDraw = new RectangleTool(this.colorControl1.getSelectedColor(), penWidth);
                        tmpDraw.newPoint(x, y);
                        break;
                    case Tool.FILL:
                        tmpDraw = new FillTool(this.colorControl1.getSelectedColor(), paintImage.Image);
                        break;
                    case Tool.ERASER:
                        tmpDraw = new EraserTool(Color.White, penWidth);
                        tmpDraw.newPoint(x, y);
                        break;
                    case Tool.STAMPER:
                        if (selectStampDlg.getSelectedImage()!=null)
                        {
                            tmpDraw = new StampTool(Color.White, selectStampDlg.getSelectedImage());
                            tmpDraw.newPoint(x, y);
                        }
                        break;
                    case Tool.MOVE:
                        for (int i = drawHistory.Count - 1; i >= 0; i--)
                        {
                            DrawTool dTool = drawHistory[i];
                            if (dTool.contains(x, y, 0, 0, 1, 0))
                            {
                                if (dTool is EraserTool || dTool is FillTool)
                                    break;
                                tmpDraw = new MoveTool(dTool, i);
                                tmpDraw.newPoint(x, y);
                                drawHistory.RemoveAt(i);
                                redrawHistory();
                                break;
                            }
                        }
                        break;
                    case Tool.RESIZE:
                        for (int i = drawHistory.Count - 1; i >= 0; i--)
                        {
                            DrawTool dTool = drawHistory[i];
                            if (dTool.contains(x, y, 0, 0, 1, 0))
                            {
                                if (dTool is EraserTool || dTool is FillTool)
                                    break;
                                tmpDraw = new ResizeTool(dTool, i);
                                tmpDraw.newPoint(x, y);
                                drawHistory.RemoveAt(i);
                                redrawHistory();
                                break;
                            }
                        }
                        break;
                    case Tool.ROTATE:
                        for (int i = drawHistory.Count - 1; i >= 0; i--)
                        {
                            DrawTool dTool = drawHistory[i];
                            if (dTool.contains(x, y, 0, 0, 1, 0))
                            {
                                if (dTool is EraserTool || dTool is FillTool)
                                    break;
                                tmpDraw = new RotateTool(dTool, i);
                                tmpDraw.newPoint(x, y);
                                drawHistory.RemoveAt(i);
                                redrawHistory();
                                break;
                            }
                        }
                        break;
                    case Tool.TEXT:
                        tmpDraw = new TextTool(this.colorControl1.getSelectedColor(), penWidth);
                        tmpDraw.newPoint(x, y);
                        break;
                }
            }
            if (!(tmpDraw is FillTool))
            {
                drawTmp();
            }
            this.Cursor = Cursors.Default;
        }

        private void paintImage_MouseUp(object sender, MouseEventArgs e)
        {
            if (tmpDraw == null || this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            int x, y;

            if (paintImage.Image.Width / paintImage.Image.Height > paintImage.Width / paintImage.Height)
            {
                x = e.X * paintImage.Image.Width / paintImage.Width;
                y = (e.Y - (paintImage.Height - paintImage.Image.Height * paintImage.Width /
                    paintImage.Image.Width) / 2) * paintImage.Image.Width / paintImage.Width;
            }
            else
            {
                x = (e.X - (paintImage.Width - paintImage.Image.Width * paintImage.Height /
                    paintImage.Image.Height) / 2) * paintImage.Image.Height / paintImage.Height;
                y = e.Y * paintImage.Image.Height / paintImage.Height;
            }
            if (tmpDraw is FillTool)
            {
                this.Cursor = Cursors.WaitCursor;
                if (tmpDraw.newPoint(x, y))
                {
                    drawHistory.Add(tmpDraw);
                    drawNew(tmpDraw);
                }
                this.Cursor = Cursors.Default;
                tmpDraw = null;
            }
            else if (tmpDraw is TextTool)
            {
                TextInputDialog textDlg = new TextInputDialog("Tekstityökalu", "Kirjoita teksti", "Ok", "Peru");
                textDlg.ShowDialog();
                if (textDlg.DialogResult != System.Windows.Forms.DialogResult.Cancel)
                {
                    TextTool textTool = (TextTool)tmpDraw;
                    textTool.setText(textDlg.WrittenText);
                    textTool.setReady();
                    drawHistory.Add(tmpDraw);
                    drawNew(tmpDraw);
                    tmpDraw = null;
                }
            }
            else
            {
                drawHistory.Add(tmpDraw);
                drawNew(tmpDraw);
                tmpDraw = null;
            }
            this.Cursor = Cursors.Default;

        }

        private void paintImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (tmpDraw == null || selectedTool == Tool.FILL || tmpDraw is FillTool)
            {
                return;
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                int x, y;

                if (paintImage.Image.Width / paintImage.Image.Height > paintImage.Width / paintImage.Height)
                {
                    x = e.X * paintImage.Image.Width / paintImage.Width;
                    y = (e.Y - (paintImage.Height - paintImage.Image.Height * paintImage.Width /
                        paintImage.Image.Width) / 2) * paintImage.Image.Width / paintImage.Width;
                }
                else
                {
                    x = (e.X - (paintImage.Width - paintImage.Image.Width * paintImage.Height /
                        paintImage.Image.Height) / 2) * paintImage.Image.Height / paintImage.Height;
                    y = e.Y * paintImage.Image.Height / paintImage.Height;
                }
                tmpDraw.newPoint(x, y);
            }
            drawTmp();
        }

        private void btnUndo_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (tmpDraw != null)
            {
                tmpDraw = null;
                drawTmp();
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            if (drawHistory.Count > 0)
            {
                DrawTool dTool = drawHistory[drawHistory.Count - 1];
                if (dTool is MoveTool)
                {
                    drawHistory.Insert(((MoveTool)dTool).getOriginalToolIndex(), ((MoveTool)dTool).getOriginalTool());
                }
                if (dTool is ResizeTool)
                {
                    drawHistory.Insert(((ResizeTool)dTool).getOriginalToolIndex(), ((ResizeTool)dTool).getOriginalTool());
                }
                if (dTool is RotateTool)
                {
                    drawHistory.Insert(((RotateTool)dTool).getOriginalToolIndex(), ((RotateTool)dTool).getOriginalTool());
                }
                drawHistory.RemoveAt(drawHistory.Count - 1);
            }
            tmpDraw = null;
            redrawHistory();
            this.Cursor = Cursors.Default;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            DialogResult dr = InformationDialog.Show("Haluatko varmasti tyhjentää?", MessageBoxButtons.YesNo);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                drawHistory.Clear();
                tmpDraw = null;
                originalImage.Dispose();
                originalImage = new Bitmap(IMAGE_SIZE, IMAGE_SIZE * paintImage.Height / paintImage.Width);
                Graphics g = Graphics.FromImage(originalImage);
                g.Clear(Color.White);
                g.Dispose();
                this.currentImage = (Bitmap)originalImage.Clone();
                this.paintImage.Image = (Bitmap)originalImage.Clone();
                maxPenWidth = Math.Max(originalImage.Width, originalImage.Height) / 20;
                updatePenWidth();
            }
            this.Cursor = Cursors.Default;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            DialogResult dr = selectImageDlg.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                drawHistory.Clear();
                tmpDraw = null;
                originalImage.Dispose();
                originalImage = new Bitmap(selectImageDlg.getSelectedImage());
                this.currentImage = (Bitmap)originalImage.Clone();
                this.paintImage.Image = (Bitmap)originalImage.Clone();
                maxPenWidth = Math.Max(originalImage.Width, originalImage.Height) / 20;
                updatePenWidth();
            }
        }

        private void btnEraser_Click(object sender, EventArgs e)
        {
            selectTool(Tool.ERASER);
        }

        private void paintImage_MouseEnter(object sender, EventArgs e)
        {
            if (this.Cursor != Cursors.WaitCursor)
            {
                this.Cursor = actionCursor;
            }
        }

        private void paintImage_MouseLeave(object sender, EventArgs e)
        {
            if (this.Cursor != Cursors.WaitCursor)
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            penWidth = (int)(penWidth * 1.5f + 1);
            updatePenWidth();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            penWidth = (int)(penWidth / 1.5f - 1);
            updatePenWidth();
        }

        private void updatePenWidth()
        {
            if (penWidth < minPenWidth)
                penWidth = minPenWidth;
            if (penWidth > maxPenWidth)
                penWidth = maxPenWidth;
            updatePenWidthBox();
            if (tmpDraw != null)
            {
                tmpDraw.setPenWidth(penWidth);
                drawTmp();
            }
        }

        private void updatePenWidthBox()
        {
            if (this.pbLineWidth.Image == null)
            {
                this.pbLineWidth.Image = new Bitmap(this.maxPenWidth * 3, this.maxPenWidth * 3);
            }
            float penSize;

            if ((paintImage.Width / paintImage.Height) < (paintImage.Image.Width / paintImage.Image.Height))
            {
                penSize = penWidth * paintImage.Width / paintImage.Image.Width;
                Console.Out.WriteLine("penWidth: " + penWidth + ", penSize: " + penSize);
                Console.Out.WriteLine("paintImage.Width: " + paintImage.Width + ", paintImage.Image.Width: " + paintImage.Image.Width);
            }
            else
            {
                penSize = penWidth * paintImage.Height / paintImage.Image.Height;
                Console.Out.WriteLine("penWidth: " + penWidth + ", penSize: " + penSize);
                Console.Out.WriteLine("paintImage.Height: " + paintImage.Height + ", paintImage.Image.Height: " + paintImage.Image.Height);
            }

            Graphics g = Graphics.FromImage(this.pbLineWidth.Image);
            if (this.colorControl1.getSelectedColor() == SystemColors.Control)
            {
                g.Clear(SystemColors.ControlText);
                g.DrawLine(new Pen(this.colorControl1.getSelectedColor(), penSize),
                    0, this.pbLineWidth.Image.Height / 2,
                    this.pbLineWidth.Image.Width, this.pbLineWidth.Image.Height / 2);
            }
            else
            {
                g.Clear(SystemColors.Control);
                g.DrawLine(new Pen(this.colorControl1.getSelectedColor(), penSize),
                    0, this.pbLineWidth.Image.Height / 2,
                    this.pbLineWidth.Image.Width, this.pbLineWidth.Image.Height / 2);
            }
            g.Dispose();
            this.pbLineWidth.Refresh();
        }

        private void btnStamper_Click(object sender, EventArgs e)
        {
            selectStampDlg.ShowDialog();
            selectTool(Tool.STAMPER);
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            selectTool(Tool.MOVE);
        }

        private void btnResize_Click(object sender, EventArgs e)
        {
            selectTool(Tool.RESIZE);
        }

        private void btnRotate_Click(object sender, EventArgs e)
        {
            selectTool(Tool.ROTATE);
        }

        private void btnTextTool_Click(object sender, EventArgs e)
        {
            selectTool(Tool.TEXT);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (tmpDraw != null && tmpDraw is TextTool)
            {
                if (keyData == Keys.Return || keyData == Keys.Enter)
                {
                    if (((TextTool)tmpDraw).setReady())
                    {
                        drawHistory.Add(tmpDraw);
                        drawNew(tmpDraw);
                    }
                    tmpDraw = null;
                    drawNew(tmpDraw);
                }
                else if (keyData == Keys.Back)
                {
                    ((TextTool)tmpDraw).deleteChar();
                }
                drawTmp();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void DrawForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (tmpDraw != null && tmpDraw is TextTool && !Char.IsControl(e.KeyChar))
            {
                ((TextTool)tmpDraw).addChar(e.KeyChar);
                drawTmp();
            }
        }

        private void currentColorImage_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.AnyColor = true;
            cd.AllowFullOpen = true;
            cd.SolidColorOnly = true;
            cd.FullOpen = true;
            if (cd.ShowDialog() == DialogResult.OK)
            {
                Color color = cd.Color;
                if (color.ToArgb() == FillTool.dontColor.ToArgb())
                {
                    if (color.B >= 255)
                        color = Color.FromArgb(color.A, color.R, color.G, color.B - 1);
                    else
                        color = Color.FromArgb(color.A, color.R, color.G, color.B + 1);
                }
                this.currentColorImage.BackColor = color;
                this.colorControl1.setSelectedColor(color);
            }
        }
    }
}
